import cardplayer
import carddealer
import carddeck


def _show_options(player, dealer):
    options = ["(S)tand", "(H)it"]
    if len(player.current_hand) == 2:
        options.append("Su(R)render")
        if dealer.current_hand[1].face == 1:
            options.append('(I)nsurance')
        if player.current_hand[0].face == player.current_hand[1].face:
            options.append("(D)double Down")
            
    return ' | '.join(options)
            
if __name__ == '__main__':
    
    names = []
    print "Welcome to BlackJack"
    num_players = raw_input("How many players (max 6)?>")
    
    for i in range(int(num_players)):
        names.append(raw_input("Enter name for Player{0}>".format(i+1)))
#        
    deck = carddeck.Deck(carddeck.DECKSIZE)
    deck.shuffle()
    
#    names = ['David', 'Angela']
    players = []
    for name in names:
        players.append(cardplayer.Player(name))
    dealer = carddealer.Dealer()
    
    
    #GET BETS FOR PLAYERS
    while True:
        for player in players:
            while True:
                bet = raw_input("{0} Place bet ({1})>".format(player.name, player.bank))
                try:
                    player.current_bet=int(bet)
                    break
                except ValueError:
                    "Invalid bet"
                  
        #DEAL CARDS TO PLAYERS   
        for player in players:
            player.new_hand(deck.deal_card(), deck.deal_card())
            player.display_hand()
            if player.total == 21:
                print "BLACKJACK!!!"
            
            
        #DEAL TO DEALER
        dealer.new_hand(deck.deal_card(), deck.deal_card())  
        dealer.display_hand()
        
        #Main Loop
        for player in players:
            
            while not player.is_standing and not player._21 and not player._blackjack:
                player.display_hand()
                
                p_choice = raw_input("{0}>".format(_show_options(player, dealer)))
                p_choice.lower()
                if p_choice == 's':
                    player.stand()
                if p_choice == 'h':
                    player.hit(deck.deal_card())
                    if player.is_bust:
                        break
    
        dealer.display_full_hand()
        while not dealer.is_standing:
            if dealer.total >= 17:
                dealer.stand()
            else:
                dealer.hit(deck.deal_card())
                dealer.display_full_hand()
                if dealer.is_bust:
                    break
        
        dealer.display_full_hand()
        
        print "\n\nRound Summary"
        for player in players:
            print "Dealer: {0}, Player: {1}".format(dealer.total, player.total)
            if player.is_bust:
                print "{0} bust. -{1}. New balance:{2}".format(player.name, player.current_bet, player.bank)
                
            else:
                if dealer.is_bust:
                    player.bank += player.current_bet * 2
                    print "{0} Wins. +{1}, New Balance: {2}".format(player.name, player.current_bet, player.bank)
                    
                elif player.total == dealer.total:
                    player.bank += player.current_bet
                    print "{0} push. Balance: {1}".format(player.name, player.bank)
                    
                elif player.total < dealer.total:
                    print "{0} looses. -{1} New Balance: {2}".format(player.name, player.current_bet, player.bank)
                    
                else:
                    player.bank += player.current_bet * 2
                    print "{0} Wins. +{1}, New Balance: {2}".format(player.name, player.current_bet, player.bank)
        
        for player in players:
            player.reset()
        dealer.reset()
        
        again = raw_input("\nPlay again?>")
        if not again.lower() == 'y':
            break
        